#!/usr/bin/env python

import paramiko
import fabric
import MySQLdb
import sys
import pymongo
import gearman
import thread

class Monitoring:
    
    #Mongo Db Connection Parameters
    #conn = pymongo.Connection('mongodb://admin:password@localhost:27017/admin')
    #db = conn['cronmon']
    #coll = db['login']    
    #m = ('kgrvamsi')
    #doc = coll.find_one({'aid' : m})
    #print doc['lname']  
    
    #Mysql Database Connection Parameters
#con = MySQLdb.connect(host='localhost', user='root', passwd='redhat',
 #                             db='monitor')
        
    
        restart = "service apache2 restart"
        start = "service apache2 start"
        stop = "service apache2 stop"        
    
    
def apacheservice(x):
    
        if x == restart:
                ssh = paramiko.SSHClient()
                #Adds the ssh Keys with Auto Confirmation
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect('localhost', username='root', password='password')                
                stdin, stdout, stderr = ssh.exec_command(x)
                print stdout.readlines()
                ssh.close()
                return restart
        elif x == start:
                ssh = paramiko.SSHClient()
                #Adds the ssh Keys with Auto Confirmation
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect('localhost', username='root', password='password')            
                stdin, stdout, stderr = ssh.exec_command(x)
                print stdout.readlines()
                ssh.close()
                return start
        elif x == stop:
                ssh = paramiko.SSHClient()
                #Adds the ssh Keys with Auto Confirmation
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect('localhost', username='root', password='password')                
                stdin, stdout, stderr = ssh.exec_command(x)
                print stdout.readlines()
                ssh.close()
                return stop
            
d = { 0 : 'apacheversion', 1 : 'mysqlversion', 2 : 'pythonversion', 3 : 'javaversion'}
b = {'apacheversion': 'apache2 -v','mysqlversion': 'mysql -V','pythonversion': 'python -V'}
            
def serverinfo(x):
                            
        if x == d[0]:
        
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect('localhost', username='root', password='password')
                stdin, stdout, stderr = ssh.exec_command(b['apacheversion'])
                print stdout.readlines()
                ssh.close()
                        
        elif x == d[1]:
        
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect('localhost', username='root', password='password')
                stdin, stdout, stderr = ssh.exec_command(b['mysqlversion'])
                print stdout.readlines()
                ssh.close() 
    
        elif x == d[2]:
                
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect('localhost', username='root', password='password')
                stdin, stdout, stderr = ssh.exec_command(b['mysqlversion'])
                print stdout.readlines()
                ssh.close()         
        
        


#to get a single row from the db we use fetchone    
#row = cur.fetchone()
#to get multiple rows from the db we use fetchall
#row =cur.fetchall()
#print row[4]

#user = str(row[0])
#state = str(row[4])

conn = pymongo.Connection('mongodb://admin:password@localhost:27017/admin')
db = conn['cronmon']
coll = db['execution']    

#doc = coll.find({'aid' : m}) 



#if user == 'ACTIVE':
#    print "Job started for user ",user
    
    
#initializing the Worker 
worker = gearman.GearmanWorker(['localhost:4730'])
con = MySQLdb.connect(host='localhost', user='root', passwd='redhat',
                              db='monitor')
cur = con.cursor()
cur.execute("SELECT * FROM Execution where state='Active'")
rows =cur.fetchall()

for row in rows:
        userlist=str(row[0])
        worker.set_client_id(userlist)
    
#List of Functions To get Executed when there is any job scheduled to the work to do    

worker.register_task('apacheversion',serverinfo)
worker.register_task('mysqlversion',serverinfo)
worker.register_task('pythonversion',serverinfo)
worker.register_task('javaversion',serverinfo)
worker.register_task('restart',apacheservice)
worker.register_task('stop',apacheservice)
worker.register_task('start',apacheservice)
worker.work()
    
    

   
    
    
            



